#include "Renderer_PCH.h"
#include "TextMgr.h"

/*--------------------------------------------------------------------------*/

TextMgr::TextMgr() :
	m_textColor(NULL),
	m_backgroundColor(NULL),
	m_textMaterial(NULL)
{

}

/*--------------------------------------------------------------------------*/

TextMgr::~TextMgr()
{

}

/*--------------------------------------------------------------------------*/

void TextMgr::InitTextMgr()
{
	m_textColor = (glm::vec4*) g_Renderer->GetRendererStorageMgr()->CreateConst("TextColor", EConst::Float4, 1, this, 0)->GetPointer();
	m_backgroundColor = (glm::vec4*) g_Renderer->GetRendererStorageMgr()->CreateConst("TextBackgroundColor", EConst::Float4, 1, this, 1)->GetPointer();

	SetTextColor(glm::vec4(1.0, 0.0, 1.0, 1.0));
	SetBackgroundColor(glm::vec4(1.0, 1.0, 0.0, 1.0));

	g_Renderer->AddRequest(NULL, new MaterialRequest(DATA_PATH "engine/material/text.mat"));
}

/*--------------------------------------------------------------------------*/

void TextMgr::WriteText( RFont* font, const std::string& text, const glm::vec2& position )
{	
	CheckPointers();

	if (font == NULL)
	{
		font = m_defaultFont;
	}
	
	const uint length = text.size();
	float cursor = 0.0f;

	DynamicSurface* ds = g_Renderer->GetDynGeometryMgr()->BeginGeometry(length, EVertex::Position0 | EVertex::Color0 | EVertex::Color1);
	for (uint i = 0; i < length; ++i)
	{
		Char& c = font->m_chars[text[i]];

		ds->vPosition0()[i].x = position.x + cursor;
		ds->vPosition0()[i].y = position.y;
		ds->vColor0()[i] = c.m_source;
		ds->vColor1()[i] = c.m_destination;

		cursor += c.m_xadvance;
	}

	RVertexArray* vertexarray = g_Renderer->GetDynGeometryMgr()->EndGeometry();
	MaterialMgr* matMgr = g_Renderer->GetMaterialMgr();

	//matMgr->Push();
	vertexarray->Bind(NULL);
	matMgr->Bind(m_textMaterial);
	matMgr->SetSampler("fontTexture", font->m_texture, NULL);

	glDrawArrays(GL_POINTS, 0, length);

	delete vertexarray;

	//matMgr->Pop();
}

/*--------------------------------------------------------------------------*/

void TextMgr::SetTextColor( const glm::vec4& color )
{
	*m_textColor = color;
}

/*--------------------------------------------------------------------------*/

void TextMgr::SetBackgroundColor( const glm::vec4& color )
{
	*m_backgroundColor = color;
}

/*--------------------------------------------------------------------------*/

void TextMgr::UpdateConst( uint arg )
{
	switch (arg)
	{
	case 0: // TextColor
		{
			break;
		}
	case 1: // TextBackgroundColor
		{
			break;
		}
	}
}

/*--------------------------------------------------------------------------*/

void TextMgr::CheckPointers()
{
	if (m_textMaterial == NULL)
	{
		m_textMaterial = g_Renderer->GetRendererStorageMgr()->GetMaterial("text_2d");
		__Check(m_textMaterial);
	}

	if (m_defaultFont == NULL)
	{
		m_defaultFont = g_Renderer->GetRendererStorageMgr()->GetFont("default");
		__Check(m_defaultFont);
	}
}

/*--------------------------------------------------------------------------*/
